var tcity = require("../../../utils/city.js");

var app = getApp()
Page({
  data: {
    provinces: [],
    province: "",
    citys: [],
    city: "",
    countys: [],
    county: '',
    value: [0, 0, 0],
    values: [0, 0, 0],
    condition: false,
    // 
    inp_cargo: "",
    inp_call: "",
    inp_ress: "",
    number_fal: 2,
    mo: false,
    address:null,
  },
  bindChange: function (e) {
    //console.log(e);
    var val = e.detail.value
    var t = this.data.values;
    var cityData = this.data.cityData;

    if (val[0] != t[0]) {
      console.log('province no ');
      const citys = [];
      const countys = [];

      for (let i = 0; i < cityData[val[0]].sub.length; i++) {
        citys.push(cityData[val[0]].sub[i].name)
      }
      for (let i = 0; i < cityData[val[0]].sub[0].sub.length; i++) {
        countys.push(cityData[val[0]].sub[0].sub[i].name)
      }

      this.setData({
        province: this.data.provinces[val[0]],
        city: cityData[val[0]].sub[0].name,
        citys: citys,
        county: cityData[val[0]].sub[0].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], 0, 0]
      })

      return;
    }
    if (val[1] != t[1]) {
      console.log('city no');
      const countys = [];

      for (let i = 0; i < cityData[val[0]].sub[val[1]].sub.length; i++) {
        countys.push(cityData[val[0]].sub[val[1]].sub[i].name)
      }

      this.setData({
        city: this.data.citys[val[1]],
        county: cityData[val[0]].sub[val[1]].sub[0].name,
        countys: countys,
        values: val,
        value: [val[0], val[1], 0]
      })
      return;
    }
    if (val[2] != t[2]) {
      console.log('county no');
      this.setData({
        county: this.data.countys[val[2]],
        values: val
      })
      return;
    }


  },
  open: function () {
    this.setData({
      condition: !this.data.condition
    })
  },
  save: function (own) {
    console.log(own);
  },
  onLoad: function (options) {
    console.log(options);
    this.setData({
      address:options.id,
    })
    var that = this;

    tcity.init(that);

    var cityData = that.data.cityData;


    const provinces = [];
    const citys = [];
    const countys = [];

    for (let i = 0; i < cityData.length; i++) {
      provinces.push(cityData[i].name);
    }
    console.log('省份完成');
    for (let i = 0; i < cityData[0].sub.length; i++) {
      citys.push(cityData[0].sub[i].name)
    }
    console.log('city完成');
    for (let i = 0; i < cityData[0].sub[0].sub.length; i++) {
      countys.push(cityData[0].sub[0].sub[i].name)
    }

    that.setData({
      'provinces': provinces,
      'citys': citys,
      'countys': countys,
      'province': cityData[0].name,
      'city': cityData[0].sub[0].name,
      'county': cityData[0].sub[0].sub[0].name
    })
    console.log('初始化完成');


  },
  // 获取input值
  cargo: function (e) {
    this.setData({
      inp_cargo: e.detail.value,
    })
  },
  call: function (e) {
    this.setData({
      inp_call: e.detail.value,
    })
  },
  ress: function (e) {
    this.setData({
      inp_ress: e.detail.value,
    })
  },
  save: function (e) {

    if (this.data.inp_cargo == "" || this.data.inp_call == "" || this.data.inp_ress == "") {
      wx.showModal({
        title: '提示',
        content: '输入不可以为空',
      });
      return;
    } else {
      wx.request({
        url: 'https://bag.idianwei.com/index.php/Api/User/editAddress',
        data: {
          userId: 16,
          token: 1501221705,
          addressId: this.data.address,
          userName: this.data.inp_cargo,
          userPhone: this.data.inp_call,
          areaId1: this.data.province,
          areaId2: this.data.city,
          areaId3: this.data.county,
          address: this.data.inp_ress,
          isDefault: this.data.number_fal,
        },
        success: function (res) {
          console.log(res);
          wx.navigateTo({
            url: '/pages/index/set_1/set',
          });
        }
      });

    }



  },
  mo: function () {
    if (this.data.mo == false) {
      this.setData({
        mo: true,
        number_fal: 2,
      })
    } else {
      this.setData({
        mo: false,
        number_fal: 1,
      })
    }
  }

})